﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.DocManger;

using System.Web.UI.HtmlControls;

using Jzy.Core.Common.Web;
using Jzy.Model.DocManger;
using Frame.Lib.Enums;
using Frame.Lib.Common;
using Frame.Lib.Utilities;

namespace Jzy.Web.DocManager.Dir
{
    public partial class List : PageList
    {
        private DirBO _dir;
        private DirBO Dir
        {
            get
            {
                if (_dir == null)
                {
                    _dir = new DirBO();
                }
                return _dir;
            }
        }

        protected bool IsPrivate
        {
            get
            {
                if (string.IsNullOrEmpty(Request["IsPrivate"]))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        // private List<int> lstDirEditIDs;//当前用户可拥有编辑权限的ID集合   


        private string strOrders;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindView();
            }
            AddAttr();
            strSelectIDs = hidSelectIDs.Value;
            hidSelectIDs.Value = "";
            strOrders = hidOrders.Value;
            hidOrders.Value = "";

            JSUtil.SetTableHeight("list", 80);

            thRight.Visible = (!IsPrivate);
        }
        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "目录?如果目录下有文档，将删除失败，请确认");
            //JSUtil.MoveTree(btnMove, TableName.DocDir, hidSelectIDs, btnRefresh);
            string url = "MoveDlg.aspx?";
            if (IsPrivate)
            {
                url += "IsPrivate=1&";
            }

            btnMove.Attributes["onclick"] = "return  moveDir('" + url + "','" + hidSelectIDs.ClientID + "'," + btnRefresh.ClientID + ")";

            JSUtil.ClickBtn(lnkDelete, btnDelete);
            JSUtil.ClickBtn(lnkMove, btnMove);
            JSUtil.ClickBtn(lnkSaveOrders, btnSaveOrders);
        }

        private void BindView()
        {
            LVUtil.Init(lv, lvHeader);
            IEnumerable<DirInfo> list = Dir.GetTreeList();

            //私有文档
            if (IsPrivate)
            {
                list = list.Where(T => T.UpperID == 0 || (T.CreatorID == AppSession.UserID && T.IsPrivate == true));
                list.Where(T => T.UpperID == 0).First().Name = "我的文档";
            }
            else
            {
                list = list.Where(T => T.IsPrivate == false);
            }


            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID", "UpperID" };
            lv.DataBind();


            if (IsPostBack)
            {
                LVUtil.RestoreTreeListState(lv, hidHideTrIDs.Value);
            }

        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                string strID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString();

                string url = GlobalVar.WebCurrPath + "Edit.aspx";
                LVUtil.SetEdit(sender, e, 460, 251, url);
                HtmlTableRow row = null;
                foreach (Control ctl in e.Item.Controls)
                {
                    if (ctl is HtmlTableRow)
                    {
                        row = (HtmlTableRow)ctl;
                        break;
                    }
                }
                row.Attributes["id"] = strID;
                string upperID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["UpperID"].ToString();
                row.Attributes.Add("UpperID", upperID);

                CheckBox chkSelect = (CheckBox)e.Item.FindControl("chkSelect");
                if (upperID == Constants.TREE_ROOT_UPPER_ID.ToString())
                {
                    chkSelect.Enabled = false;
                }

                string strUrl = GlobalVar.WebCurrPath + "Edit.aspx?UpperID=" + StrUtil.EncryptUrl(strID);
                if (IsPrivate)
                {
                    strUrl += "&IsPrivate=true";
                }
                HyperLink lnkAdd = (HyperLink)e.Item.FindControl("hypAdd");
                JSUtil.ShowForm(lnkAdd, "增加栏目", null, strUrl, 460, 251);

                HyperLink lnkSetRight = (HyperLink)e.Item.FindControl("lnkSetRight");
                lnkSetRight.Style.Add("cursor", "pointer");
                lnkSetRight.Enabled = false;

                //设置目录权限
                strUrl = "SaveDirRight.aspx?DirID=" + strID;
                lnkSetRight.Enabled = true;

                JSUtil.ShowForm(lnkSetRight, "设置权限", null, strUrl, 300, 319);

            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }
            try
            {
                this.Dir.dbOper.BeginTrans();
                List<int> ids = StrUtil.SplitToInt(strSelectIDs);
                Dir.Delete(ids.ToArray());
                BindView();

                this.Dir.dbOper.CommitTrans();

                string strScript = "top.document.getElementsByName('frameLeft')[0].contentWindow.document.getElementById('ctl00_ContentPlaceHolder1_btnRefresh').click();";

                JSUtil.ExcuteJS(strScript);
            }
            catch (Exception)
            {
                this.Dir.dbOper.RollbackTrans();
                JSUtil.Alert("删除失败，请确认被删除目录下文档是否被清空");
            }
        }

        protected void btnSaveOrders_Click(object sender, EventArgs e)
        {
            List<int> ids = new List<int>();
            List<int> orders = new List<int>();
            StrUtil.SplitIDOrders(ids, orders, strOrders);
            Dir.SaveOrders(ids.ToArray(), orders.ToArray());
            BindView();
        }
    }
}
